const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const ManagerModel = require('../../model/managerModel');
const tools = require('../../model/tools');

router.get('/', async (req, res)=>{

  let result = await ManagerModel.find({});
  res.render('admin/manager/index.html',{
    list: result
  })

});

router.get('/add',(req, res)=>{

  res.render('admin/manager/add.html');
});

router.post('/doAdd', async (req, res)=>{

  // 1.获取数据
  let username = req.body.username;
  let password = req.body.password;
  let rpassword = req.body.rpassword;
  let email = req.body.email;
  let mobile = req.body.mobile;
  let status = req.body.status;

  // 2.验证数据是否合法
  if(username == ''){
    res.render('admin/public/error.html', {
      redirectUrl: `/${req.app.locals.adminPath}/manager/add`,
      message: '用户名不能为空'
    })
    return;
  }else if(password.length < 6){
    res.render('admin/public/error.html', {
      redirectUrl: `/${req.app.locals.adminPath}/manager/add`,
      message: '密码长度不能小于6位'
    })
    return;
  }else if(password != rpassword){
    res.render('admin/public/error.html', {
      redirectUrl: `/${req.app.locals.adminPath}/manager/add`,
      message: '密码和确认密码不一致'
    })
    return;
  }

  // 3.判断数据库中是否有我们要增加的数据
  let result = await ManagerModel.find({ username: username });
  if(result.length > 0){
    res.render('admin/public/error.html', {
      redirectUrl: `/${req.app.locals.adminPath}/manager/add`,
      message: '当前用户已经存在'
    })
    return;
  }else{
    let addResult = new ManagerModel({
      username,
      password: tools.md5(password),
      email,
      mobile,
      status,
      add_time: tools.getUnix()
    });

    await addResult.save();
    res.redirect(`/${req.app.locals.adminPath}/manager`)
  }
});

router.get('/edit', async (req, res)=>{

  // 获取要修改的数据的id
  let id = req.query.id;
  var result = await ManagerModel.find({ _id: id });
  if(result.length > 0 ){
    res.render('admin/manager/edit.html', {
      list: result[0]
    });
  }else{
    res.redirect(`/${req.app.locals.adminPath}/manager`);
  }
});

router.post('/doEdit', async (req, res)=>{
  // 1.获取数据
  let id = req.body.id;
  let password = req.body.password;
  let rpassword = req.body.rpassword;
  let email = req.body.email;
  let mobile = req.body.mobile;
  let status = req.body.status;

  // 2.判断密码是否修改
  if(password.length > 0){

    if(password.length < 6){
      res.render('admin/public/error',{
        message: '密码长度不能小于6位',
        redirectUrl: `/${req.app.locals.adminPath}/manager/edit?id=${id}`
      });
      return;
    }else if(password != rpassword){
      res.render('admin/public/error.html', {
        redirectUrl: `/${req.app.locals.adminPath}/manager/edit?id=${id}`,
        message: '密码和确认密码不一致'
      })
      return;
    }else{
      // 修改密码
      await ManagerModel.updateOne({_id: id}, {
        password: tools.md5(password),
        email,
        mobile,
        status
      })
    }

  }else{
    // 不修改密码
    await ManagerModel.updateOne({_id: id}, {
      email,
      mobile,
      status
    })
  }
  res.redirect(`/${req.app.locals.adminPath}/manager`);
});

router.get('/delete', async (req, res)=>{
  // 1.获取数据
  let id = req.body.id;

  // 2.执行删除
  await ManagerModel.deleteOne({id: id});
  res.render('admin/public/success.html', {
    message: '删除数据成功',
    redirectUrl: `/${req.app.locals.adminPath}/manager`
  });
});

module.exports = router;